package com.qijian.tool.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.utils.ObjectUtils;
import com.qijian.common.utils.SecurityUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.tool.domain.Msg;
import com.qijian.tool.domain.query.MsgQuery;
import com.qijian.tool.service.IMsgService;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 我的信息Controller
 *
 * @author qijian
 * @date 2022-06-06
 */
@Api(tags = "我的信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/tool/msg")
public class MsgController extends BaseController {

    private final IMsgService msgService;

    /**
     * 查询我的信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MsgQuery query, IPage<Msg> page) {
        query.setUserId(SecurityUtils.getUserId());
        return getDataTable(msgService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }


    /***
     * 我的消息列表(新)
     * @param query
     * @param page
     * @return
     */
    @GetMapping("/listVo")
    public TableDataInfo listVo(MsgQuery query, IPage<Msg> page) {
        if (!"admin".equals(SecurityUtils.getLoginUser().getUser().getUserName())) {
            query.setUserId(SecurityUtils.getUserId());
        }
        return getDataTable(msgService.pageVo(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /***
     * 登录用户未处理消息total
     * @return
     */
    @GetMapping("/total")
    public AjaxResult listTotal() {
        MsgQuery query = new MsgQuery();
        if (!"admin".equals(SecurityUtils.getLoginUser().getUser().getUserName())) {
            query.setUserId(SecurityUtils.getUserId());
        }
        query.setIsClose(false);
        return AjaxResult.success(msgService.count(WhereEntityTool.invoke(query)));
    }

    /**
     * 获取我的消息
     */
    @GetMapping("/group")
    public AjaxResult group(MsgQuery query) {
        if (!"admin".equals(SecurityUtils.getLoginUser().getUser().getUserName())) {
            query.setUserId(SecurityUtils.getUserId());
        }
        List<Msg> msgList = msgService.list(WhereEntityTool.invoke(query));
        Map<String, List<Msg>> sourceTypeMap = msgList.stream().collect(Collectors.toMap(Msg::getSourceType, Lists::newArrayList, ListUtils::union));
        return AjaxResult.success(sourceTypeMap);
    }


    /**
     * 获取我的消息
     */
    @GetMapping("/groupNew")
    public TableDataInfo groupNew(MsgQuery query, Page<Msg> page) {
        page.orders().add(OrderItem.desc("create_time"));
        query.setUserId(SecurityUtils.getUserId());
        Page<Msg> pageList = msgService.page(page, WhereEntityTool.invoke(query));
        return getDataTable(pageList);
    }

    /**
     * 获取我的信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(msgService.getById(id));
    }

    @PutMapping(value = "/close/{id}")
    public AjaxResult close(@PathVariable("id") Long id) {
        Assert.notNull(id);
//        Msg msg = new Msg();
        Msg msg = msgService.getById(id);
        // 非处理人点击不更改消息状态
        if (ObjectUtils.notEqual(SecurityUtils.getUserId(), msg.getUserId())) {
            return AjaxResult.success();
        }
        msg.setId(id);
        msg.setIsClose(true);
        msg.setIsRead(true);
        msgService.updateById(msg);
        return AjaxResult.success();
    }

    @PutMapping(value = "/closeAll")
    public AjaxResult closeAll() {
        msgService.update(new LambdaUpdateWrapper<Msg>()
                .set(Msg::getIsClose, true)
                .set(Msg::getIsRead, true)
                .eq(Msg::getIsClose, false)
                .eq(Msg::getIsRead, false).
                eq(!"admin".equals(SecurityUtils.getLoginUser().getUser().getUserName()), Msg::getUserId, SecurityUtils.getUserId())
        );
        return AjaxResult.success();
    }

    @PutMapping(value = "/removeAll")
    public AjaxResult removeAll() {
        msgService.update(new LambdaUpdateWrapper<Msg>()
                .set(Msg::getIsDel, true)
                .eq(Msg::getIsClose, true)
                .eq(Msg::getIsRead, true).
                eq(!"admin".equals(SecurityUtils.getLoginUser().getUser().getUserName()), Msg::getUserId, SecurityUtils.getUserId())
        );
        return AjaxResult.success();
    }
    
    @PostMapping(value = "/rePush2APP/{id}")
    public AjaxResult rePush2APP(@PathVariable("id") Long id) {
        return AjaxResult.success(msgService.rePush2APP(id));
    }

}
